<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <jsp:include page="../include.jsp" flush="true"/>
	<script type="text/javascript">
	var grid;
	var win;
	var url;
	/**/
	$(function() {
		grid = $('#user_info').datagrid();
		$('#btn-save,#btn-cancel').linkbutton();
		win = $('#user-window').window( {
			collapsible : false,//折叠
			minimizable : false,//最小化
			maximizable : false,//最大化
			draggable : true,//拖动
			resizable : false,//改变大小
			modal : true,
			closed : true
		});

	});

	function append() {
		reject();
		win.window('open');
		status = 'enable';
		$('#userform').form('clear');
		$("#task_id").attr({readonly:false, title:""});
		url ='c01!addTask';
	}
	function remove() {
		var rows = grid.datagrid('getSelected');
		if(!rows){
			show('请先选择一行数据!');
			return false;
		}
		$.messager.confirm('提示','确定要删除吗?',function(result){  
            if (result){  
                $.post('c01!removeTask?task_id='+rows.task_id,function(message){
                	show(message.returnMsg);
                    $('#user_info').datagrid('reload');
                });  
            } 
            reject();
        });
	}
	function update() {
		var row = grid.datagrid('getSelected');
		if (row) {
			win.window('open');
			$('#userform').form('clear');
			$('#userform').form('load', row);
			url = 'c01!updateTask';
			$("#task_id").attr({readonly:"readonly", title:"只读"});
		} else {
			show('请先选择一行数据!');
		}
	}
	function save() {
		$('#userform').form('submit', {
			url : url,
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(data) {
				grid.datagrid('reload');
				win.window('close');
			}
		});
	}
	//选题
	function confirmTask() {//checkConfirmTask
		if(user_role_id != 3 && user_role_id != 6){
			$.post('c01!checkConfirmTask',
				{user_id:user_id},
				function(message){
					if(message.returnFlag = 'N'){
						alert(message.returnMsg);
					}else{
						alert('只有组长才可以选择小组课题!');
					}
		        }
	        );
			return false;
		}
		var rows = grid.datagrid('getSelected');
		if(!rows){
			show('请先选择一行数据!');
			return false;
		}
		$.post('c01!checkConfirmTask',
			{
				user_id:user_id
			},
			function(message){
				if(message.returnFlag == 'N'){//已选
					show(message.returnMsg);
					return false;
				}else if(message.returnFlag == 'Y'){
					$.messager.confirm('提示','课题选后无法修改,确定选择课题'+rows.task_name+'吗?',function(result){
			           if (result){
			                $.post('c01!confirmTask',
			                {
			                	user_id:user_id,
			                	task_id:rows.task_id
			                },
			                function(message){
			                	show(message.returnMsg);
			                    $('#user_info').datagrid('reload');
			                });  
			           } 
			           reject();
			        });
				}
			}
	    );
	}
	
	function query(){
		var params = $('#user_info').datagrid('options').queryParams;
        var fields =$('#query-form').serializeArray();
        $.each( fields, function(i, field){
            params[field.name] = field.value;
        });   
        $('#user_info').datagrid('reload');
	}
	function reject(){
		$('#user_info').datagrid('clearSelections');
	} 
	function closeWindow() {
		win.window('close');
		reject();
	}
</script>
	<script type="text/javascript">
		$(function(){
			var lastIndex;
			$('#user_info').datagrid({
				width:'auto',
				height:'auto',
				method:'post',
				fit:true,
				nowrap: false,
				fitColumns:'true',
				striped: true,
				url:'c01!queryTask?user_role_id='+user_role_id+'&user_id='+user_id,
				sortName: 'task_id',
				idField:'task_id',
				sortOrder: 'desc',//列排序
				columns:[[
					{field:'ck',checkbox:true,width:2}, //显示复选框
					{field:'task_id',title:'课题简称',align:'center',width:80},
					{field:'task_name',title:'课题全称',align:'center',editor:'text',width:120},
					{field:'task_note',title:'课题描述',align:'center',editor:'text',width:120},
					{field:'task_major_id',title:'专业',align:'center',editor:'text',width:20,hidden:true},
					{field:'task_faculty_id',title:'学院',align:'center',editor:'text',width:20,hidden:true},
					{field:'major_name',title:'所属专业',align:'center',editor:'text',width:120},
					{field:'faculty_name',title:'所属学院',align:'center',editor:'text',width:120},
					{field:'task_year',title:'学年',align:'center',editor:'text',width:50}
				]],
				rownumbers:true,
				singleSelect:true,
				toolbar:toolbar,
				onLoadSuccess:function(){  
               		$('#user_info').datagrid('clearSelections');
            	}
			});
		});
		
	</script>
  </head>
  <body>
  	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',split:false,border:true" title="" style="height:40px">
			<form id="query-form" method="post" style="padding-left:10px;">
		  		课题简称<input type="text" class="easyui-validatebox" name="task_id" style="width:90px;">&nbsp;
		  		课题全称<input type="text" class="easyui-validatebox" name="task_name" style="width:90px;"></input>&nbsp;
				课题描述<input type="text" class="easyui-validatebox" name="task_note" style="width:90px;"></input>&nbsp;
				所属专业<input class="easyui-combobox" name="task_major_id" id="task_major_id" style="width:90px;"
										data-options="
												url:'c01!queryMajor',
												mode:'remote',
												valueField:'MAJOR_ID',
												textField:'MAJOR_NAME',
												panelHeight:'auto'
									">&nbsp;
				所属学院<input class="easyui-combobox" name="task_faculty_id" id="task_faculty_id" style="width:90px;"
										data-options="
												url:'c01!queryFaculty',
												mode:'remote',
												valueField:'FACULTY_ID',
												textField:'FACULTY_NAME',
												panelHeight:'auto'
									">&nbsp;
				学年<input name="taskYearStart" class="easyui-numberspinner" style="width:80px;" value=""
		        				data-options="min:2013,max:2099,editable:true,increment:2">
		        	至<input name="taskYearEnd" class="easyui-numberspinner" style="width:80px;" value=""
		        				data-options="min:2013,max:2099,editable:true,increment:2"">&nbsp;
		        <a href="javascript:void(0)" onclick="query()" id="btn-save" icon="icon-search">查询</a>
		  	</form>
		</div>
		<div data-options="region:'center',border:false" title="课题信息列表">
			<table id="user_info"></table>
			<div id="user-window" title="用户窗口" style="width:450px;height:auto;">
				<div style="padding:20px 20px 40px 80px;">
					<form id="userform" method="post" >
						<table>
							<tr>
								<td>课题简称：</td>
								<td><input type="text" class="easyui-validatebox" name="task_id" id="task_id" required="true"></input></td>
							</tr>
							<tr>
								<td>课题全称：</td>
								<td><input type="text" class="easyui-validatebox" name="task_name" id="task_name" required="true"></input></td>
							</tr>
							<tr>
								<td>课题描述：</td>
								<td><input type="text" class="easyui-validatebox" name="task_note" id="task_note"></input></td>
							</tr>
							<tr>
								<td>所属专业：</td>
								<td>
									<input class="easyui-combobox" name="task_major_id" id="task_major_id"
										data-options="
												url:'c01!queryMajor',
												valueField:'MAJOR_ID',
												textField:'MAJOR_NAME',
												panelHeight:'auto'
									">
								</td>
							</tr>
							<tr>
								<td>所属学院：</td>
								<td>
									<input class="easyui-combobox" name="task_faculty_id" id="task_faculty_id"
										data-options="
												url:'c01!queryFaculty',
												valueField:'FACULTY_ID',
												textField:'FACULTY_NAME',
												panelHeight:'auto'
									">
							</tr>
							<tr>
								<td>学年：</td>
								<td>
									<input name="task_year" id="task_year" class="easyui-numberspinner" style="width:130px;"  
		        						required="required" data-options="min:2013,max:2099,editable:true">
		        				</td>
							</tr>
						</table>
					</form>
				</div>
				<div style="text-align:center;padding:5px;">
					<a href="javascript:void(0)" onclick="save()" id="btn-save" icon="icon-save">确认</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeWindow()" id="btn-cancel" icon="icon-cancel">取消</a>
				</div>
			</div>
		</div>
	</div>
	<!-- ======================================================== -->
	<script type="text/javascript" src="./jquery-easyui-1.3.2/easyloader.js"></script>
	<script type="text/javascript">
		var user_role_id = <%=session.getAttribute("user_role_id")%>
		if(user_role_id == 1 || user_role_id == 5){
			var toolbar = [{
				id:'addbtn',
				text:'增加',
				iconCls:'icon-add',
				handler:function(){append()}
			},{
				id:'updatebtn',
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){update()}
			},{
				text:'删除',
				iconCls:'icon-cut',
				handler:function(){remove()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}else{
			var toolbar = [{
				id:'addbtn',
				text:'选题',
				iconCls:'icon-save',
				handler:function(){confirmTask()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}
	</script>
  </body>
</html>
